package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.List;
import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.erp.I_OrderRelation;

import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderRelation;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class OrderRelationDAO  extends ModifiedLogDAO<OrderRelation>{

	public static String LIST_SQL="SELECT T.ID FROM ORDERS T LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID  LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID LEFT OUTER JOIN PEOPLE PE ON T.REFERENCEPERSON = PE.ID LEFT OUTER JOIN LOCATIONS SE ON T.LOCATIONREF1=SE.ID ";

	
	static FindOrdersInfoDAO findOrdersInfoDAO;
	
	public OrderRelationDAO(I_Session s,FindOrdersInfoDAO findOrdersInfoDAO) {
		super(s);
		this.findOrdersInfoDAO=findOrdersInfoDAO;
	}

	public OrderRelationDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		 return new TableDefinition(s,
                 "ORDERRELATIONS",
                 new Field[]{
                      new Field(I_OrderRelation.ID,Datas.STRING,Formats.STRING),
                      new Field(I_OrderRelation.ORDERID,Datas.STRING,Formats.STRING,Order.class),
                      new Field(I_OrderRelation.REFID,Datas.STRING,Formats.STRING),
                      new Field(I_OrderRelation.MEMO,Datas.STRING,Formats.STRING),
                      new Field(I_OrderRelation.REFBEFORESTATE,Datas.STRING,Formats.STRING),
                      new Field(I_OrderRelation.REFAFTERSTATE,Datas.STRING,Formats.STRING),
                      new Field(I_OrderRelation.LASTMODIFIED,Datas.TIMESTAMP,Formats.TIMESTAMP),
                      new Field(I_OrderRelation.CASH,Datas.DOUBLE,Formats.DOUBLE)
                     },
                 new int[]{0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, OrderRelation obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getSuperID());
		dp.setString(3,obj.getRefId());
		dp.setString(4,obj.getMemo());
		dp.setString(5,obj.getBeforState());
		dp.setString(6,obj.getAfterState());
		dp.setTimestamp(7,obj.getLastModified());
		dp.setDouble(8,obj.getCash());
		
	}

	@Override
	public OrderRelation readValues(DataRead dr,OrderRelation obj) throws BasicException {
		if(obj==null)
			obj=new OrderRelation();
		obj.setId(dr.getString(1));
		obj.setSuperID(dr.getString(2));
		obj.setRefId(dr.getString(3));
		obj.setMemo(dr.getString(4));
		obj.setBeforState(dr.getString(5));
		obj.setAfterState(dr.getString(6));
		obj.setLastModified(dr.getTimestamp(7));
		obj.setCash(dr.getDouble(8));
		if(findOrdersInfoDAO!=null)
		  obj.setRelOrderInfo(findOrdersInfoDAO.find(obj.getRefId()));
		return obj;
	}

	@Override
	public Class getSuportClass() {
		return OrderRelation.class;
	}
	
    public List<OrderRelation> listOrderRelationInfoByOrder(String id) throws BasicException {
        return new PreparedSentence(s,getTableDefinition().getListSQL()+ " WHERE ORDERID=? ", SerializerWriteString.INSTANCE, this).list(id);
    }

    public List<OrderRelation> listOrderRelationInfoByRefOrder(String id) throws BasicException {
    	return new PreparedSentence(s,getTableDefinition().getListSQL()+ " WHERE REFID=?", SerializerWriteString.INSTANCE, new OrderRefRead()).list(id);
    }

    @Override
    public List<OrderRelation> list(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
        		getTableDefinition().getListSQL()+ " WHERE ORDERID IN("+LIST_SQL+ "WHERE ?(QBF_FILTER) "+")", new String[]{"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE", "T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME", "T.LOCATIONREF", "T.TOTALPRICE"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.DOUBLE}), this).list(filter);
    }
    
    public static class OrderRefRead implements SerializerRead<OrderRelation>{
    	@Override
    	public OrderRelation readValues(DataRead dr) throws BasicException {
    		OrderRelation obj=new OrderRelation();
    		obj.setId(dr.getString(1));
    		obj.setSuperID(dr.getString(2));
    		obj.setRefId(dr.getString(3));
    		obj.setMemo(dr.getString(4));
    		obj.setBeforState(dr.getString(5));
    		obj.setAfterState(dr.getString(6));
    		obj.setLastModified(dr.getTimestamp(7));
    		obj.setCash(dr.getDouble(8));
    		if(findOrdersInfoDAO!=null)
    		   obj.setRelOrderInfo(findOrdersInfoDAO.find(obj.getSuperID()));
    		return obj;
    	}
    }
}
